﻿Imports System.Data
Imports System.Data.SqlClient

Partial Public Class Departamento
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub btnguardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnguardar.Click
        Dim sCnn As String
        sCnn = "Data Source=MARCE-PC\SQLEXPRESS;Initial Catalog=tesis;integrated security=SSPI;persist security info=false;"
        Dim cn As New SqlConnection
        cn.ConnectionString = sCnn
        cn.Open()
        Dim ins As New SqlCommand
        ins.Connection = cn
        ins.CommandType = CommandType.StoredProcedure
        ins.CommandText = "sp_GuardarDepartamento"
        ins.Parameters.AddWithValue("@cod_departamento", Trim(txtcoddpto.Text))
        ins.Parameters.AddWithValue("@nombre_departamento", Trim(txtnombre.Text))
        ins.Parameters.AddWithValue("@cod_provincia", Trim(txtprovincia.Text))
        ins.ExecuteNonQuery()
        MsgBox("Los datos se guardaron con éxito", MsgBoxStyle.Exclamation)
        cn.Close()
    End Sub

    Protected Sub btnmodificar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnmodificar.Click
        Dim sCnn As String
        sCnn = "Data Source=MARCE-PC\SQLEXPRESS;Initial Catalog=tesis;integrated security=SSPI;persist security info=false;"
        Dim cn As New SqlConnection
        cn.ConnectionString = sCnn
        cn.Open()
        Dim mdf As New SqlCommand
        mdf.Connection = cn
        mdf.CommandType = CommandType.StoredProcedure
        mdf.CommandText = "sp_ModificarDepartamento"
        mdf.Parameters.AddWithValue("@nombre_departamento", Trim(txtnombre.Text))
        mdf.Parameters.AddWithValue("@cod_provincia", Trim(txtprovincia.Text))
        mdf.ExecuteNonQuery()
        MsgBox("Los datos se modificaron con éxito", MsgBoxStyle.Exclamation)
        cn.Close()
        txtcoddpto.Text = ""
        txtprovincia.Text = ""

    End Sub

    Protected Sub btneliminar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btneliminar.Click
        Dim sCnn As String
        sCnn = "Data Source=MARCE-PC\SQLEXPRESS;Initial Catalog=tesis;integrated security=SSPI;persist security info=false;"
        Dim cn As New SqlConnection
        cn.ConnectionString = sCnn
        cn.Open()
        Dim com As New SqlCommand
        com.Connection = cn
        com.CommandType = CommandType.StoredProcedure
        com.CommandText = "sp_EliminarDepartamento"
        com.Parameters.AddWithValue("@cod_departamento", txtcoddpto.Text)
        com.ExecuteNonQuery()
        MsgBox("Los datos se eliminaron con éxito", MsgBoxStyle.Exclamation)
        cn.Close()
        txtcoddpto.Text = ""
    End Sub

    Protected Sub btnbuscar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnbuscar.Click
        Dim ds2 As DataSet
        Dim da As SqlDataAdapter
        Dim cn As New SqlConnection
        Dim scnn As String = "data source= MARCE-PC\SQLEXPRESS; initial catalog= tesis; integrated security= SSPI; persist security info= false;"
        Dim busqueda As String = txtbuscar.Text()
        cn = New SqlConnection(scnn)
        da = New SqlDataAdapter("sp_ConsultaDGDepartamento", cn)
        da.SelectCommand.CommandType = CommandType.StoredProcedure

        da.SelectCommand.Parameters.Add(New SqlParameter("@cod_departamento", SqlDbType.Int, 8))
        da.SelectCommand.Parameters("@cod_departamento").Value = Trim(busqueda)
        ds2 = New DataSet()
        da.Fill(ds2, "DEPARTAMENTO")
        GridView1.DataSource = ds2.Tables("DEPARTAMENTO").DefaultView
        GridView1.DataBind()
        da.Dispose()
        cn.Close()
    End Sub
End Class